using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for DMListItem objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DMListItem.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestDMListItem
    {
        [SetUp]
        public override void SetupTest()
        {
            //Runs every time that any testmethod is executed
            base.SetupTest();
        }

        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            base.SetupFixture();
        }

        [TearDown]
        public override void TearDownTest()
        {
            //runs every time any testmethod is complete
            base.TearDownTest();
        }
        [Test]
        public void Test_ToString()
        {
            //---------------Set up test pack-------------------
            DMListItem dmListItem = TestUtilsDMListItem.CreateUnsavedValidDMListItem();
            //---------------Assert Precondition----------------
            TestUtilsShared.AssertStringNotEmpty(dmListItem.Display, "dmListItem.Display");
            //---------------Execute Test ----------------------
            string actualToString = dmListItem.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmListItem.Display + " - " + dmListItem.Value, actualToString);
        }

        [Test]
        public void Test_ToString_DisplayIsNull()
        {
            //---------------Set up test pack-------------------
            DMListItem dmListItem = TestUtilsDMListItem.CreateUnsavedValidDMListItem();
            dmListItem.Display = "";
            //---------------Assert Precondition----------------
            TestUtilsShared.AssertStringEmpty(dmListItem.Display, "dmListItem.Display");
            //---------------Execute Test ----------------------
            string actualToString = dmListItem.ToString();
            //---------------Test Result -----------------------
                        Assert.AreEqual(dmListItem.Display + " - " + dmListItem.Value, actualToString);

        }
        [Test]
        public void Test_ToString_ValueIsNull()
        {
            //---------------Set up test pack-------------------
            DMListItem dmListItem = TestUtilsDMListItem.CreateUnsavedValidDMListItem();
            dmListItem.Value = "";
            //---------------Assert Precondition----------------
            TestUtilsShared.AssertStringEmpty(dmListItem.Value, "dmListItem.Value");
            //---------------Execute Test ----------------------
            string actualToString = dmListItem.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmListItem.Display + " - " + dmListItem.Value, actualToString);

        }

        [Test]
        public void Test_ToString_DisplayIsNull_ValueIsNull()
        {
            //---------------Set up test pack-------------------
            DMListItem dmListItem = TestUtilsDMListItem.CreateUnsavedValidDMListItem();
            dmListItem.Display = "";
            dmListItem.Value = "";
            //---------------Assert Precondition----------------
            TestUtilsShared.AssertStringEmpty(dmListItem.Display, "dmListItem.Display");
            //---------------Execute Test ----------------------
            string actualToString = dmListItem.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(dmListItem.ListItemID.ToString(), actualToString);
        }
        [Test]
        public void Test_ToString_LookupListIsNull_IDIsNull()
        {
            //---------------Set up test pack-------------------
            DMListItem dmListItem = TestUtilsDMListItem.CreateUnsavedValidDMListItem();
            dmListItem.Display = "";
            dmListItem.Value = "";
            dmListItem.ListItemID = null;
            //---------------Assert Precondition----------------
            TestUtilsShared.AssertStringEmpty(dmListItem.Display, "dmListItem.Display");
            Assert.IsNull(dmListItem.ListItemID);
            //---------------Execute Test ----------------------
            string actualToString = dmListItem.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual("-unnamed DMListItem-", actualToString);
        }
        [Test]
        public void Test_InsertNewItemInfo_Invalid()
        {
            //---------------Set up test pack-------------------
            DMListItem dmListItem = new DMListItem();

            //---------------Assert Precondition----------------
            Assert.IsTrue(dmListItem.Status.IsNew);

            //---------------Execute Test ----------------------
            try
            {
                dmListItem.Save();
                Assert.Fail("expected Err");
            }
            //---------------Test Result -----------------------
            catch (BusObjectInAnInvalidStateException ex)
            {
                StringAssert.Contains("Display' is a compulsory field and has no value", ex.Message);
                StringAssert.Contains("Simple Lookup List ID' is a compulsory field and has no value", ex.Message);
                StringAssert.Contains("Value' is a compulsory field and has no value", ex.Message);
            }
            Assert.IsTrue(dmListItem.Status.IsNew);
        }

        [Test]
        public void Test_InsertNewItemInfo()
        {
            //---------------Set up test pack-------------------
            DMListItem dmListItem = TestUtilsShared.CreateUnsavedValidItemInfo();

            //---------------Assert Precondition----------------
            Assert.IsTrue(dmListItem.Status.IsNew);

            //---------------Execute Test ----------------------
            dmListItem.Save();

            //---------------Test Result -----------------------
            Assert.IsFalse(dmListItem.Status.IsNew);
        }

        [Test]
        public void Test_UpdateItemInfo()
        {
            //---------------Set up test pack-------------------
            DMListItem dmListItem = TestUtilsShared.CreateSavedItemInfo();

            //---------------Assert Precondition----------------
            Assert.IsFalse(dmListItem.Status.IsNew);
            string message;
            Assert.IsTrue(dmListItem.IsEditable(out message));

            //---------------Execute Test ----------------------
            TestUtilsShared.UpdateItemInfoWithValidData(dmListItem);

            //---------------Test Result -----------------------
            Assert.IsFalse(dmListItem.Status.IsNew);
        }

        [Test]  // Ensures that updates to property values are stored and can be retrieved
        public void Test_UpdateDMListItem()
        {
            CheckIfTestShouldBeIgnored();
            //---------------Set up test pack-------------------
            DMListItem dMListItem = TestUtilsDMListItem.CreateSavedDMListItem();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            object valueForValue = TestUtilsShared.GetRandomString();
            dMListItem.Value = (System.String)valueForValue;
            object valueForDisplay = TestUtilsShared.GetRandomString();
            dMListItem.Display = (System.String)valueForDisplay;
            object valueForGenerated = TestUtilsShared.GetRandomBoolean();
            dMListItem.Generated = (System.Boolean)valueForGenerated;
            object valueForSimpleLookupListID = TestUtilsDMLookupListSimple.CreateUnsavedValidDMLookupListSimple().SimpleLookupListID;
            //object valueForSimpleLookupListID = TestUtilsDMSimpleLookupList.CreateUnsavedValidDMSimpleLookupList().SimpleLookupListID;
            dMListItem.SimpleLookupListID = (System.Guid)valueForSimpleLookupListID;
            dMListItem.Save();

            //---------------Test Result -----------------------
            BusinessObjectManager.Instance.ClearLoadedObjects();
            GC.Collect();
            TestUtilsDomain.WaitForGC();
            DMListItem retrievedDMListItem =
                    Broker.GetBusinessObject<DMListItem>(dMListItem.ID);

            Assert.AreEqual(valueForValue, retrievedDMListItem.Value);
            Assert.AreEqual(valueForDisplay, retrievedDMListItem.Display);
            Assert.AreEqual(valueForGenerated, retrievedDMListItem.Generated);
            Assert.AreEqual(valueForSimpleLookupListID, retrievedDMListItem.SimpleLookupListID);
        }

        [Test]  // Ensures that gets and sets in the code refer to the same property
        public void Test_PropertyGetters()
        {
            CheckIfTestShouldBeIgnored();
            //---------------Set up test pack-------------------
            DMListItem dMListItem = new DMListItem();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            object valueForValue = TestUtilsShared.GetRandomString();
            dMListItem.Value = (System.String)valueForValue;
            object valueForDisplay = TestUtilsShared.GetRandomString();
            dMListItem.Display = (System.String)valueForDisplay;
            object valueForGenerated = TestUtilsShared.GetRandomBoolean();
            dMListItem.Generated = (System.Boolean)valueForGenerated;
            object valueForSimpleLookupListID = TestUtilsDMLookupListSimple.CreateUnsavedValidDMLookupListSimple().SimpleLookupListID;
           // object valueForSimpleLookupListID = TestUtilsDMSimpleLookupList.CreateUnsavedValidDMSimpleLookupList().SimpleLookupListID;
            dMListItem.SimpleLookupListID = (System.Guid)valueForSimpleLookupListID;

            //---------------Test Result -----------------------
            Assert.AreEqual(valueForValue, dMListItem.Value);
            Assert.AreEqual(valueForDisplay, dMListItem.Display);
            Assert.AreEqual(valueForGenerated, dMListItem.Generated);
            Assert.AreEqual(valueForSimpleLookupListID, dMListItem.SimpleLookupListID);
        }

        [Test]  // Ensures that property getters in the code point to the correct property
        public void Test_PropertyGettersUseCorrectPropertyNames()
        {
            CheckIfTestShouldBeIgnored();
            //---------------Set up test pack-------------------
            DMListItem dMListItem = new DMListItem();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            object valueForValue = TestUtilsShared.GetRandomString();
            dMListItem.Value = (System.String)valueForValue;
            object valueForDisplay = TestUtilsShared.GetRandomString();
            dMListItem.Display = (System.String)valueForDisplay;
            object valueForGenerated = TestUtilsShared.GetRandomBoolean();
            dMListItem.Generated = (System.Boolean)valueForGenerated;
            object valueForSimpleLookupListID = TestUtilsDMLookupListSimple.CreateUnsavedValidDMLookupListSimple().SimpleLookupListID;
          //  object valueForSimpleLookupListID = TestUtilsDMSimpleLookupList.CreateUnsavedValidDMSimpleLookupList().SimpleLookupListID;
            dMListItem.SimpleLookupListID = (System.Guid)valueForSimpleLookupListID;

            //---------------Test Result -----------------------
            Assert.AreEqual(valueForValue, dMListItem.GetPropertyValue("Value"));
            Assert.AreEqual(valueForDisplay, dMListItem.GetPropertyValue("Display"));
            Assert.AreEqual(valueForGenerated, dMListItem.GetPropertyValue("Generated"));
            Assert.AreEqual(valueForSimpleLookupListID, dMListItem.GetPropertyValue("SimpleLookupListID"));
        }

        [Test]  // Ensures that property setters in the code point to the correct property
        public void Test_PropertySettersUseCorrectPropertyNames()
        {
            CheckIfTestShouldBeIgnored();
            //---------------Set up test pack-------------------
            DMListItem dMListItem = new DMListItem();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            object valueForValue = TestUtilsShared.GetRandomString();
            dMListItem.SetPropertyValue("Value", valueForValue);
            object valueForDisplay = TestUtilsShared.GetRandomString();
            dMListItem.SetPropertyValue("Display", valueForDisplay);
            object valueForGenerated = TestUtilsShared.GetRandomBoolean();
            dMListItem.SetPropertyValue("Generated", valueForGenerated);
            object valueForSimpleLookupListID = TestUtilsDMLookupListSimple.CreateUnsavedValidDMLookupListSimple().SimpleLookupListID;
       //     object valueForSimpleLookupListID = TestUtilsDMSimpleLookupList.CreateUnsavedValidDMSimpleLookupList().SimpleLookupListID;
            dMListItem.SetPropertyValue("SimpleLookupListID", valueForSimpleLookupListID);

            //---------------Test Result -----------------------
            Assert.AreEqual(valueForValue, dMListItem.Value);
            Assert.AreEqual(valueForDisplay, dMListItem.Display);
            Assert.AreEqual(valueForGenerated, dMListItem.Generated);
            Assert.AreEqual(valueForSimpleLookupListID, dMListItem.SimpleLookupListID);
        }

        [Test]  // Checks that BOs in a single relationship load correctly (no tampering with class defs)
        public void Test_LoadThroughSingleRelationship_SimpleLookupList()
        {
            CheckIfTestShouldBeIgnored();
            //---------------Set up test pack-------------------
            BORegistry.DataAccessor = new DataAccessorInMemory();
            DMListItem dMListItem = TestUtilsDMListItem.CreateUnsavedValidDMListItem();
            dMListItem.LookupListSimple = TestUtilsDMLookupListSimple.CreateUnsavedValidDMLookupListSimple();
            Assert.NotNull(dMListItem.LookupListSimple, "The List should have a simple lookup list");

            dMListItem.LookupListSimple.Save();

            DMLookupListSimple boForRelationshipLookupListSimple = dMListItem.LookupListSimple;

            BusinessObjectManager.Instance.ClearLoadedObjects();
            GC.Collect();
            TestUtilsDomain.WaitForGC();
            //---------------Execute Test ----------------------
            DMLookupListSimple loadedRelatedBO = Broker.GetBusinessObject<DMLookupListSimple>(boForRelationshipLookupListSimple.ID);
            DMListItem loadedDMListItem = Broker.GetBusinessObject<DMListItem>(dMListItem.ID);
            //---------------Test Result -----------------------
            Assert.AreEqual(boForRelationshipLookupListSimple, loadedDMListItem.LookupListSimple);
            Assert.AreEqual(loadedRelatedBO, loadedDMListItem.LookupListSimple);
            Assert.AreEqual(loadedRelatedBO, dMListItem.LookupListSimple);
        }
    }
}
